package com.rt.easyjava.common.rsa;

import org.apache.commons.codec.binary.Base64;

import java.util.Map;

public class RSATester {

    static String publicKey;
    static String privateKey;

    static {
        try {
            Map<String, Object> keyMap = RSAUtils.genKeyPair();
            publicKey = RSAUtils.getPublicKey(keyMap);
            privateKey = RSAUtils.getPrivateKey(keyMap);
            System.err.println("公钥: \n\r" + publicKey);
            System.err.println("私钥： \n\r" + privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        //test();
        //testSign();

        // decodeByPrivate();

        String s = "admin1234__@#!#@__abc12345644445_@#!#@_2021-11-14 20:05:26.284";
        System.out.println(s.split("_@#!#@_")[0]);
        System.out.println(s.split("_@#!#@_")[1]);
    }

    static void decodeByPrivate() throws Exception {
        String privateKey = "";
        String data = "o0HtNh+oH1cSKBbm5fVLz5nUqb/pWHGZEiXEfxmg1O5MRT9zlBj+s6cXBZSJExrWzYdH5S5YexFUo9YKFpsvNxaQBROapWDC4oTpKCrnb7CLWPMpyG7UvKi++sKy0GsD6bupNOzXgAqtL0UfD5qQ4c846oJyK6ZRCHH/Wy/1d/E=";
        // data = "DaBOB+yP62MWD+4mtRnRBvA+k3torBCEokK0uVGHc/nBInrwOYzwN5Tv+8sAs1hQtOH5hMbL43SBaCXyd2aeKWkbKt5eEA+ASOaAwCdkJp85c2cc/GR72Brjn7LqSmIyHZhBHEm+O3WZQfV6RWw4Ub+mXYmwqsWgp7kVotQHl0g=";
        System.out.println(Base64.decodeBase64(data));
        byte[] mingByte = RSAUtils.decryptByPrivateKey(Base64.decodeBase64(data),privateKey);
        System.out.println(mingByte);
        System.out.println(new String(mingByte, "utf-8"));
    }

    static void test() throws Exception {
        System.err.println("公钥加密——私钥解密");
        String source = "这是一行没有任何意义的文字，你看完了等于没看，不是吗？";
        System.out.println("\r加密前文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);
        System.out.println("加密后文字：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);
        String target = new String(decodedData);
        System.out.println("解密后文字: \r\n" + target);
    }

    static void testSign() throws Exception {
        System.err.println("私钥加密——公钥解密");
        String source = "这是一行测试RSA数字签名的无意义文字";
        System.out.println("原文字：\r\n" + source);
        byte[] data = source.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        System.out.println("加密后：\r\n" + new String(encodedData));
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        System.out.println("解密后: \r\n" + target);
        System.err.println("私钥签名——公钥验证签名");
        String sign = RSAUtils.sign(encodedData, privateKey);
        System.err.println("签名:\r" + sign);
        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
        System.err.println("验证结果:\r" + status);
    }

}
